package eirkdbd.lab1.data;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * Provides functionality to load team data from db and return result as List of
 * team
 * 
 */
public class TeamDataLoader {

	private String url = "";
	private String user = "";
	private String pass = "";
	private static Connection conn;

	public TeamDataLoader() throws IOException {
		setDbCredentials(getDbCredentialsString());
	}

	/**
	 * Sets db credentials from dbCredentialsString
	 * 
	 * @param dbCredentialsString
	 */
	private void setDbCredentials(String dbCredentialsString) {
		String[] dbCredentials = dbCredentialsString.split("#");
		this.url = dbCredentials[0];
		user = dbCredentials[1];
		pass = dbCredentials[2];
	}

	/**
	 * Gets dbCredentialsString from resource file
	 * 
	 * @return
	 * @throws IOException
	 */
	private String getDbCredentialsString() throws IOException {
		InputStream is = this.getClass().getResourceAsStream("/eirkdbd/lab1/data/dbCredentials");
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		String dbCredentialsString = br.readLine();
		br.close();
		return dbCredentialsString;
	}

	/**
	 * Returns Team List from database
	 * 
	 * @return List<Team> from database
	 * @throws SQLException
	 * @throws ConnectException
	 */
	public List<Team> getTeamData() throws SQLException, ConnectException {
		try {
			startConnection();
		} catch (Exception e) {
			e.printStackTrace();
			throw new ConnectException("Cannot connect to database");
		}
		return createTeamList(getTeamsResultSet());
	}

	/**
	 * Starts connection with database
	 * 
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	private void startConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		conn = DriverManager.getConnection(url, user, pass);
	}

	/**
	 * Extracts teams from database and return appropriate result set
	 * 
	 * @return
	 * @throws SQLException
	 */
	private ResultSet getTeamsResultSet() throws SQLException {
		String query = "SELECT * from PLAYERS";
		Statement st = conn.createStatement();
		ResultSet rs = st.executeQuery(query);
		return rs;
	}

	/**
	 * Creates team list from ResultSet
	 * @param rs
	 * @return
	 * @throws SQLException
	 */
	private List<Team> createTeamList(ResultSet rs) throws SQLException {
		List<Team> teams = new ArrayList<Team>();
		while (rs.next()) {
			Team newTeam = new Team();
			newTeam.setPlayer(rs.getString("zawodnik"));
			newTeam.setTeam(rs.getString("druzyna"));
			newTeam.setMatch(rs.getInt("mecze"));
			newTeam.setGoals(rs.getInt("gole"));
			newTeam.setAssist(rs.getInt("asysty"));
			newTeam.setPoints(rs.getInt("punkty"));
			newTeam.setPenalty(rs.getInt("kary"));
			newTeam.setAdvantage(rs.getInt("w_przewadze"));
			newTeam.setAccuracy(rs.getInt("celnosc"));
			newTeam.setWinnerGoals(rs.getInt("zwycieskie"));
			newTeam.setShoots(rs.getInt("strzaly"));
			teams.add(newTeam);
		}
		conn.close();
		return teams;
	}

}
